package com.xtool.livewallpaper.base

import android.app.Activity
import android.app.Application
import android.os.Bundle
import android.util.Log

/**
 *
 * @Author: Ricky
 * @Date: 2021/1/20
 */
object ActivityStackManager : Application.ActivityLifecycleCallbacks {
    interface OnAppStatusChangedListener {
        fun onForeground(activity: Activity?)

        fun onBackground(activity: Activity?)
    }

    private val activities = mutableListOf<Activity>()
    private val mStatusListeners = mutableListOf<OnAppStatusChangedListener>()
    private var mForegroundCount = 0
    private var mConfigCount = 0
    private var mIsBackground = false

    val currentActivity: BaseActivity?
        get() = activities.lastOrNull() as? BaseActivity

    fun isAppForeground(): Boolean {
        return !mIsBackground
    }

    fun init(application: Application) {
        application.registerActivityLifecycleCallbacks(this)
    }

    fun registerAppStatusChangedListener(listener: OnAppStatusChangedListener) {
        mStatusListeners.add(listener)
    }

    fun unregisterAppStatusChangedListener(listener: OnAppStatusChangedListener) {
        mStatusListeners.remove(listener)
    }


    fun clear() {
        activities.map { it.finish() }
        activities.clear()
    }

    override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
        if (activities.isEmpty()) {
            postStatus(activity, true)
        }
        activities.add(activity)
    }

    override fun onActivityStarted(activity: Activity) {
        Log.i("ActivityStackManager", "onActivityStarted,name=$activity")
        if (mConfigCount < 0) {
            ++mConfigCount
        } else {
            ++mForegroundCount
        }
    }

    override fun onActivityResumed(activity: Activity) {
        Log.i("ActivityStackManager", "onActivityResumed,name=$activity")
        if (mIsBackground) {
            mIsBackground = false
            postStatus(activity, true)
        }
    }

    override fun onActivityPaused(activity: Activity) {
        Log.i("ActivityStackManager", "onActivityPaused,name=$activity")
    }

    override fun onActivityStopped(activity: Activity) {
        Log.i("ActivityStackManager", "onActivityStopped,name=$activity")
        if (activity.isChangingConfigurations) {
            --mConfigCount
        } else {
            --mForegroundCount
            if (mForegroundCount <= 0) {
                mIsBackground = true
                postStatus(activity, false)
            }
        }
    }

    override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
        Log.i("ActivityStackManager", "onActivitySaveInstanceState,name=$activity")
    }

    override fun onActivityDestroyed(activity: Activity) {
        activities.remove(activity)
        Log.i("ActivityStackManager", "onActivityDestroyed,name=$activity")
    }

    private fun postStatus(activity: Activity, isForeground: Boolean) {
        if (mStatusListeners.isEmpty()) return
        for (statusListener in mStatusListeners) {
            if (isForeground) {
                statusListener.onForeground(activity)
            } else {
                statusListener.onBackground(activity)
            }
        }
    }
}